import axios from "axios";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import router from '../router';
  

const request = axios.create({
    baseURL:'http://42.192.178.221:8088/api',
    timeout:5000
})

request.interceptors.request.use(
    config=>{
        return config
    },
    error=>Promise.reject(error)
)

request.interceptors.response.use(
    (config) => {
        let value = config.data;
        if (config.status && config.status === 200) {
          if (typeof value === 'string') {
            if (value === 'timeout') {
              ElementUI.MessageBox.confirm('太长时间没有操作或操作没有权限，请进入登录页面重新登录?', '提示', {
                confirmButtonText: '确定',
                type: 'warning'
              }).then(() => {
                router.push({ name: 'login' });
              });
            }else {
              ElementUI.Message.info(value);
            }
          }
        }
        return config;
      },
      (err) => {
        let value = err.response.status;
        switch (err.response.status) {
          case 400:
            ElementUI.Message.error('语法格式有误，服务器无法理解此请求')
            break;
          case 401:
          case 403:
            ElementUI.Message.warning('请登录后尝试此操作！');
            break;
          case 404:
          case 405:
            ElementUI.Message.warning(value);
            break;
          default:
            router.push('/error');;
            break;
        }
        return err.response
      }
)

export default request